﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericTest2
{
    class Program
    {
        static void Main(string[] args)
        {
            //MyGenericArray<int> intArray = new MyGenericArray<int>(5);

            //for (int i = 0; i < 5; i++)
            //{
            //    intArray.setItem(i, i * 5);
            //}

            //for (int i = 0; i < 5; i++)
            //{
            //    Console.Write(intArray.getItem(i) + " ");
            //}
            //Console.WriteLine();

            //MyGenericArray<char> charArray = new MyGenericArray<char>(5);
            //for (int i = 0; i < 5; i++)
            //{
            //    charArray.setItem(i, (char)(i + 97));
            //}

            //for (int i = 0; i < 5; i++)
            //{
            //    Console.Write(charArray.getItem(i)+" ");
            //}
            //Console.WriteLine();



            int a = 1, b = 2;
            char c = 'h', d = 'i';
            Console.WriteLine("before calling Swap:");
            Console.WriteLine($"int 类型： a={a},b={b}");
            Console.WriteLine($"char 类型： c={c},d={d}");
            Console.WriteLine("************************");

            Swap<int>(ref a, ref b);
            Swap<char>(ref c, ref d);

            Console.WriteLine("after calling Swap:");
            Console.WriteLine($"int 类型： a={a},b={b}");
            Console.WriteLine($"char 类型： c={c},d={d}");

            Console.ReadKey();
        }

        static void Swap<T>(ref T t1, ref T t2)
        {
            T temp;
            temp = t1;
            t1 = t2;
            t2 = temp;

        }
    }

    public class MyGenericArray<T>
    {
        private T[] array;
        public MyGenericArray(int size)
        {
            array = new T[size + 1];
        }

        public T  getItem(int index)
        {
            return array[index];
        }

        public void setItem(int index,T value)
        {
            array[index] = value;
        }
    }
}
